package com.hzw.saas.service.chat.service;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.chat.IChatEventService;
import com.hzw.saas.api.chat.IChatUserGroupService;
import com.hzw.saas.api.chat.bo.ChatEventBO;
import com.hzw.saas.api.chat.bo.ChatUserGroupBO;
import com.hzw.saas.api.chat.bo.FriendInfo;
import com.hzw.saas.common.config.util.PageParam;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.common.util.SnowFlakeUtil;
import com.hzw.saas.service.chat.mapper.ChatUserGroupMapper;
import com.hzw.saas.service.chat.model.ChatUserGroup;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service("chatUserGroupService")
public class ChatUserGroupServiceImpl extends ServiceImpl<ChatUserGroupMapper, ChatUserGroup> implements IChatUserGroupService, IService<ChatUserGroup> {

    @Resource(name = "chatEventService")
    private IChatEventService chatEventService;

    @Override
    public List<ChatUserGroupBO> findAllGroupByUser(String userId) {
        if (StrUtil.isBlank(userId)){
            return Collections.emptyList();
        }
        List<ChatUserGroup> list = this.lambdaQuery().eq(ChatUserGroup::getUserId, userId).list();
        return MapperUtil.nf().mapAsList(list,ChatUserGroupBO.class);
    }

    @Override
    public List<ChatUserGroupBO> findRelationship(String userId, String friendId) {
        return null;
    }


    @Override
    public void saveBatch(List<ChatUserGroupBO> chatUserGroupBOS) {
        if(CollectionUtil.isNotEmpty(chatUserGroupBOS)){
            chatUserGroupBOS.forEach(chatUserGroupBO -> {
                if (chatUserGroupBO.getPid() == null || chatUserGroupBO.getPid().equals("")){
                    chatUserGroupBO.setPid(SnowFlakeUtil.nextIdStr());
                }
            });
            List<ChatUserGroup> chatUserGroups = MapperUtil.nf().mapAsList(chatUserGroupBOS, ChatUserGroup.class);
            //新增群组 新增群组事件因为要区分发起人和批准人，独立出去
            this.saveBatch(chatUserGroups);
        }
    }
    @Override
    public void changeUserGroupEvent(String userId,String applyId,String groupId){
        ChatEventBO chatEventBO = new ChatEventBO();
        chatEventBO.setSourceId(userId);
        chatEventBO.setEventType(25);
        chatEventBO.setTargetGroupId(groupId);
        chatEventBO.setTargetUserId(applyId);
        chatEventBO.setGroupType(2);
        chatEventBO.setPid(SnowFlakeUtil.nextIdStr());
        chatEventBO.setEventDetail(applyId+"join"+groupId);
        chatEventService.addEvent(chatEventBO);
    }
    @Override
    public IPage<FriendInfo> findFriendList(String userId, PageParam pageParam) {
        return null;
    }

    @Override
    public IPage<FriendInfo> needConfirm(String userId, PageParam pageParam) {
        return null;
    }

    @Override
    public List<ChatUserGroupBO> findNeededChatUserGroupBO(List<String> userId, List<Integer> groupType, List<Integer> status) {
        return null;
    }

    @Override
    public void updateBatch(List<ChatUserGroupBO> relationship) {
        if (CollectionUtil.isNotEmpty(relationship)){
            List<ChatUserGroup> chatUserGroups = MapperUtil.nf().mapAsList(relationship, ChatUserGroup.class);
//            this.lambdaQuery().
        }
    }

    @Override
    public List<ChatUserGroupBO> findUsersByGroupId(String groupId) {
        if (StrUtil.isBlank(groupId)){
            return Collections.emptyList();
        }
        List<ChatUserGroup> list = this.lambdaQuery().eq(ChatUserGroup::getGroupId, groupId).list();
        return MapperUtil.nf().mapAsList(list,ChatUserGroupBO.class);
    }

    @Override
    public List<ChatUserGroupBO> findByUserIdAndGroup(String groupId, String userId) {
        if (StrUtil.isBlank(groupId) || StrUtil.isBlank(userId)){
            return new ArrayList<>();
        }
        List<ChatUserGroup> list = this.lambdaQuery().eq(ChatUserGroup::getGroupId, groupId)
            .eq(ChatUserGroup::getUserId, userId).list();

        if (CollectionUtil.isNotEmpty(list)){
            return MapperUtil.nf().mapAsList(list,ChatUserGroupBO.class);
        }
        return new  ArrayList<>();
    }

    @Override
    public void deleteById(String pid) {
        this.removeById(pid);
    }

    @Override
    public void updateById(ChatUserGroupBO byUserIdAndGroup) {
        if (byUserIdAndGroup != null && byUserIdAndGroup.getPid() != null){
            ChatUserGroup map = MapperUtil.nf().map(byUserIdAndGroup, ChatUserGroup.class);
            this.updateById(map);
        }
    }

    @Override
    public void deleteByGroupId(String groupId) {
        if (StrUtil.isBlank(groupId)){
            return;
        }
        List<ChatUserGroup> list = this.lambdaQuery().eq(ChatUserGroup::getGroupId, groupId).list();
        if (CollectionUtil.isNotEmpty(list)){
                this.removeByIds(list.stream().map(ChatUserGroup::getPid).collect(Collectors.toList()));
        }
    }


}
